<?php
namespace Dashboard\Controller;

use Think\Controller;
use Think\Session\Driver\Memcache;

class ShopController extends BaseController{
    //轮播图--列表
    public function index(){
        $a = (int)($_POST["currpage"]);//跳转页数
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $totalpage = 0;//总页数
        $pagesize  = 10;//每页显示数
        $arr       = currpage("news_lun","","$pagesize","$currpage","id desc");
        $totalpage = $arr['totalpage'];
        $list      = $arr['list'];
        if($list){
            foreach ($list as $k=>$v){
               /* ``lun_id` int(11) DEFAULT NULL COMMENT '轮播的信息id',
               `type_id` int(11) DEFAULT NULL COMMENT 'mei_type表的id',
               `status` tinyint(4) DEFAULT '1' COMMENT '1课程 2专家 3资讯',*/
                if($v['status'] == 1){
                    //轮播图分类名
                    $type_name = sel("mei_type","name","id = {$v['type_id']}");
                    //产品名
                    $chan_name = sel("goods_zhu","title","id = {$v['lun_id']}");
                    $list[$k]['type_name'] = $type_name['name'];
                    $list[$k]['chan_name'] = $chan_name['title'];
                    $list[$k]['status'] = "课程";
                }
                if($v['status'] == 2){
                    //轮播图分类名
                    $type_name = sel("mei_type","name","id = {$v['type_id']}");
                    //产品名
                    $chan_name = sel("teacher","name","id = {$v['lun_id']}");
                    $list[$k]['type_name'] = $type_name['name'];
                    $list[$k]['chan_name'] = $chan_name['name'];
                    $list[$k]['status'] = "专家";
                }
                if($v['status'] == 3){
                    //轮播图分类名
                    $type_name = sel("mei_type","name","id = {$v['type_id']}");
                    //产品名
                    $chan_name = sel("news","title","id = {$v['lun_id']}");
                    $list[$k]['type_name'] = $type_name['name'];
                    $list[$k]['chan_name'] = $chan_name['title'];
                    $list[$k]['status'] = "资讯";
                }
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function lun(){
        $status = I('status');
        //1课程 2专家 3资讯
        if($status == 1){
            $info = sels("goods_zhu","id,title");
        }
        if($status == 2){
            $info = sels("teacher","id,name as title");
        }
        if($status == 3){
            $info = sels("news","id,title");
        }
        $this->ajaxReturn(array("info"=>$info));
    }
    //轮播图--添加页
    public function form(){
        $id      = I('id');
        if($id != null){
            $list = sel("news_lun","","id = {$id}");
            //1课程 2专家 3资讯
            if($list['status'] == 1){
                $info = sels("goods_zhu","id,title");
            }
            if($list['status'] == 2){
                $info = sels("teacher","id,name as title");
            }
            if($list['status'] == 3){
                $info = sels("news","id,title");
            }
        }else{
            //默认是课程
            $info = sels("goods_zhu","id,title");
        }
        $type = sels("mei_type","id,name","pid = 0");
        $this->assign("type",$type)->assign("list",$list)->assign("info",$info);
        $this->display();
    }
    //轮播图数据--添加
    public function add(){
        $data['type_id']        = I('type_id');//展示位置
        $data['status']      = I('status');//展示类型
        $data['lun_id']      = I('lun_id');//轮商品得id
        $id                   = I('id');
        //处理图片
        if($_FILES['one']['name'] != null){
            $img                 = I('img');
            unlink($img);
            $info                   = $this->imgUpload("banner/");
            $savename               = $info['one']['savename'];
            $savepath               = $info['one']['savepath'];
            $data['img']            = "Data/".$savepath.$savename;
        }
        if($id == null){
            $data['create_at'] = time();
            $result = ins("news_lun",$data);
            if($result){
                $this->success("添加成功",U('index'));}
        }else{
            $data['up_at']     = time();
            $result = up_one("news_lun",array("id"=>$id),$data);
            if($result){
                $this->success("修改成功",U('index'));
            }else{
                $this->success("没有信息变更，修改失败");
            }
        }
    }
}